package han.fetch;

import han.entity.FinanceProjectIdEntity;
import han.entity.FinanceProjectInfoEntity;
import han.proof.Cookie;
import han.proof.CookieRepository;
import han.sql.InsertThread;
import han.sql.Operator;
import han.thread.CustomThread;
import han.thread.MonitorThread;
import han.util.ListTool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @fileName: FinanceProjectInfoParser
 * @author: Han
 * @date: 2018/6/26 8:58
 * @dscription: 融资项目下载线程入口
 */
public class FinanceProjectInfo {

    /**
     * 同步锁对象
     */
    private Object krLock = new Object();
    /**
     * cookie仓库
     */
    private CookieRepository cookieRepository;
    /**
     * 数据库操作对象
     */
    private Operator operator;

    FinanceProjectInfo(CookieRepository cookieRepository) {
        this.cookieRepository = cookieRepository;
        operator = Operator.getInstance();
    }

    public static void main(String... args) {
        FinanceProjectInfo financeProjectInfo = new FinanceProjectInfo(null);
        List<FinanceProjectIdEntity> idEntityList = financeProjectInfo.deleteRepeatId();
        financeProjectInfo.operator.updateFinanceProjectId(idEntityList);

    }

    /**
     * 去除重复id
     */
    private List<FinanceProjectIdEntity> deleteRepeatId() {
        //获取所有项目id
        List<FinanceProjectIdEntity> idEntityList = operator.queryAllFinanceProjectId();
        //获取已经获取到数据的项目
        List<FinanceProjectInfoEntity> infoEntityList = operator.queryProjectInfoByCondition(2);
        List<String> infoIdList = new ArrayList<>();
        for (FinanceProjectInfoEntity entity : infoEntityList) {
            String id = entity.getProjectId();
            infoIdList.add(id);
        }
        //新的id容器
        List<FinanceProjectIdEntity> newIdEntityList = new ArrayList<>();
        List<String> idList = new ArrayList<>();
        for (FinanceProjectIdEntity idEntity : idEntityList) {
            String idEntityId = idEntity.getProjectId();
            if (idList.contains(idEntityId)) {
                continue;
            } else {
                idList.add(idEntityId);
            }
            if (!infoIdList.contains(idEntityId)) {
                newIdEntityList.add(idEntity);
            }
        }

        return newIdEntityList;
    }

    void threadStart() {
        //获取id
        List<FinanceProjectIdEntity> idEntityList = deleteRepeatId();
        //获取list工具
        ListTool<FinanceProjectIdEntity> listTool = new ListTool<>();
        //cookieList
        List<Cookie> cookieList = cookieRepository.getCookieList();
        //分开list
        List<List<FinanceProjectIdEntity>> splitIdEntityList = listTool.splitListByCount(idEntityList, cookieList.size());
        //线程池
        ThreadPoolExecutor executor = new CustomThread();
        //数据库操作线程
        InsertThread insertThread = new InsertThread();
        executor.submit(insertThread);
        //监控线程
        MonitorThread monitorThread = new MonitorThread(insertThread, executor, cookieRepository);
        executor.submit(monitorThread);
        for (int index = 0; index < splitIdEntityList.size() && index < cookieList.size(); index++) {
            List<FinanceProjectIdEntity> projectIdEntityList = splitIdEntityList.get(index);
            Cookie cookie = cookieList.get(index);
            FinanceProjectInfoThread financeProjectInfoThread = new FinanceProjectInfoThread(projectIdEntityList,
                    cookie, krLock, insertThread);
            financeProjectInfoThread.setName("Thread-融资项目信息-" + index);
            executor.submit(financeProjectInfoThread);
        }
        executor.shutdown();
    }
}
